package com.hxd.otc.module.fiat_deal.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseMvpDialogFragment;
import com.hxd.otc.common.enums.BankType;
import com.hxd.otc.module.fiat_deal.adapter.FilterAdapter;
import com.hxd.otc.module.fiat_deal.bean.BaseFilterBean;
import com.hxd.otc.module.fiat_deal.bean.FilterBankTypeBean;
import com.hxd.otc.module.fiat_deal.bean.FilterFiatBean;
import com.hxd.otc.module.fiat_deal.contract.DealFilterDialogContract;
import com.hxd.otc.module.fiat_deal.presenter.DealFilterDialogPresenter;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.utils.ResourceUtil;
import com.hxd.otc.utils.StringUtil;
import com.hxd.otc.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lzy on 2018/12/25 0025.
 * description ${交易筛选条件}
 */
public class DealFilterDialogFragment extends BaseMvpDialogFragment<DealFilterDialogPresenter> implements DealFilterDialogContract.IView {
    @BindView(R.id.recyclerView_fiats)
    RecyclerView mRecyclerViewFiats;
    @BindView(R.id.recyclerView_collection_methods)
    RecyclerView mRecyclerViewCollectionMethods;
    private FilterAdapter mFiatAdapter;
    private FilterAdapter mBankTypeAdapter;

    public static final String EXTRA_FIATS = "fiats";
    private OnConfirmListener mOnConfirmListener;

    @Override
    protected DealFilterDialogPresenter createPresenter() {
        return new DealFilterDialogPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_fragment_deal_filter;
    }

    @Override
    protected void initBundle(Bundle arguments) {
        super.initBundle(arguments);
        mPresenter.setFiats((ArrayList<String>) arguments.getSerializable(EXTRA_FIATS));
    }

    @Override
    protected void initView() {
        mRecyclerViewFiats.setLayoutManager(new GridLayoutManager(getContext(), 4));
        mFiatAdapter = new FilterAdapter(getContext());
        mRecyclerViewFiats.setAdapter(mFiatAdapter);

        mRecyclerViewCollectionMethods.setLayoutManager(new GridLayoutManager(getContext(), 4));
        mBankTypeAdapter = new FilterAdapter(getContext());
        mRecyclerViewCollectionMethods.setAdapter(mBankTypeAdapter);
    }

    @Override
    protected void initData() {
        mFiatAdapter.addAll(true, mPresenter.getFiats());
        mBankTypeAdapter.addAll(true, mPresenter.getBankTypes());
    }

    @OnClick({R.id.iv_close, R.id.tv_sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                UIUtils.dismissDialogFragment(this);
                break;
            case R.id.tv_sure:
                onSure();
                break;
        }
    }

    private void onSure() {
        if (mOnConfirmListener != null) {
            mOnConfirmListener.onConfirm(((FilterFiatBean) mFiatAdapter.getCurItem()).getItem(), ((FilterBankTypeBean) mBankTypeAdapter.getCurItem()).getValue());
        }
        UIUtils.dismissDialogFragment(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window win = getDialog().getWindow();
        // 一定要设置Background，如果不设置，window属性设置无效
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.BOTTOM;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = (int) (DataUtils.getScreenHeight(getContext()) * 0.895f);
        params.height = height;
        win.setAttributes(params);
    }

    public static DealFilterDialogFragment getInstance(ArrayList<String> fiats) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(EXTRA_FIATS, fiats);
        DealFilterDialogFragment dealFilterDialogFragment = new DealFilterDialogFragment();
        dealFilterDialogFragment.setArguments(bundle);
        return dealFilterDialogFragment;
    }

    public interface OnConfirmListener {
        void onConfirm(String fiat, String bankTypeValue);
    }

    public DealFilterDialogFragment setOnConfirmListener(DealFilterDialogFragment.OnConfirmListener onConfirmListener) {
        mOnConfirmListener = onConfirmListener;
        return this;
    }

    public DealFilterDialogFragment setCurFiat(String curFiat) {
        mPresenter.setCurFiat(curFiat);
        return this;
    }

    public DealFilterDialogFragment setCurBankTypeValue(String curBankTypeValue) {
        mPresenter.setCurBankTypeValue(curBankTypeValue);
        return this;
    }

    @Override
    protected void lazyLoad() {
        super.lazyLoad();
        if (getUserVisibleHint()) {
            onShowBefore();
        }
    }

    protected void onShowBefore() {
        LogUtils.e("onShowBefore");
        String curFiat = StringUtil.emptyIfNull(mPresenter.getCurFiat());
        FilterFiatBean filterFiatBean = new FilterFiatBean(curFiat);
        List<BaseFilterBean> items = mFiatAdapter.getItems();
        for (int i = 0; i < items.size(); i++) {
            if (filterFiatBean.equals(items.get(i))) {
                mFiatAdapter.setCurPosition(i);
                break;
            }
        }
        String curBankTypeValue = mPresenter.getCurBankTypeValue();
        FilterBankTypeBean filterBankTypeBean = null;
        if (TextUtils.isEmpty(curBankTypeValue)) {
            filterBankTypeBean = new FilterBankTypeBean(ResourceUtil.getString(R.string.all), "");
        } else {
            BankType bankType = BankType.getBankTypeByValue(Integer.valueOf(curBankTypeValue));
            filterBankTypeBean = new FilterBankTypeBean(bankType.getBankName(), bankType.getType() + "");
        }

        items = mBankTypeAdapter.getItems();
        for (int i = 0; i < items.size(); i++) {
            if (filterBankTypeBean.equals(items.get(i))) {
                mBankTypeAdapter.setCurPosition(i);
                break;
            }
        }

    }
}
